
% How many quantiles to sort loans into based on own IRR
% If I use 5, the first quantile would be -5000% & the scale is messed up

N_Quantiles = 3;
N_boots = 500;

%%%%%%%%%%%%%%%%%%%
% Only T1-L Group %
%%%%%%%%%%%%%%%%%%%

data = readtable("../data/interim/loanlevelirrT1L.csv");

T = max(data.loandayselapsed)+1;
N = height(data)/T;

cf = reshape(data.cf,T,N);

% Calculate IRRs for individual loans

irrbyloan = irr(cf);
irrbyloan(isnan(irrbyloan))=-inf;
irrbyloan = irrbyloan';

% Re-arrange loans

irrbyloan = [irrbyloan [1:length(irrbyloan)]'];
irrbyloan = sortrows(irrbyloan,1);
cf = cf(:,irrbyloan(:,2)');

% Group into quantiles

cf_quantiles = NaN(size(cf,1),N_Quantiles);
for d=1:N_Quantiles
    lowind = ceil(size(cf,2)/N_Quantiles)*(d-1)+1;
    highind = min((ceil(size(cf,2)/N_Quantiles)*d),size(cf,2));
    cf_quantiles(:,d) = sum(cf(:,lowind:highind),2);
end

% IRR by quantiles

irrbyquantiles = irr(cf_quantiles);

% IRR overall

irrall = irr(sum(cf,2));

% Export data for plotting

irrbyloan_T1L = irrbyloan;
irrbyquantiles_T1L = irrbyquantiles;

%--------------------------%
% Bootstrap & calculate SE %
%--------------------------%

irrbyquantiles_allboots = NaN(N_boots,4);

for i=1:N_boots

    cf_new = cf(:,randsample(1:size(cf,2),size(cf,2),true));

    % Calculate IRRs for individual loans

    irrbyloan = irr(cf_new);
    irrbyloan(isnan(irrbyloan))=-inf;
    irrbyloan = irrbyloan';

    % Re-arrange loans

    irrbyloan = [irrbyloan [1:length(irrbyloan)]'];
    irrbyloan = sortrows(irrbyloan,1);
    cf_new = cf_new(:,irrbyloan(:,2)');

    % Calculate IRR on a portfolio of all loans

    cf_allloanportfolio = sum(cf_new,2);
    irr_allloanportfolio = irr(cf_allloanportfolio);
    irrbyquantiles_allboots(i,4) = irr_allloanportfolio;

    % Group into quantiles

    cf_quantiles = NaN(size(cf_new,1),N_Quantiles);
    for d=1:N_Quantiles
        lowind = ceil(size(cf_new,2)/N_Quantiles)*(d-1)+1;
        highind = min((ceil(size(cf_new,2)/N_Quantiles)*d),size(cf_new,2));
        cf_quantiles(:,d) = sum(cf_new(:,lowind:highind),2);
    end

    % IRR by quantiles

    irrbyquantiles_oneboot = irr(cf_quantiles);

    irrbyquantiles_allboots(i,1:3) = irrbyquantiles_oneboot;

end

irrbyquantiles_allboots_T1L = irrbyquantiles_allboots;



%%%%%%%%%%%%%%%%%%%
% Only T2-U Group %
%%%%%%%%%%%%%%%%%%%

data = readtable("../data/interim/loanlevelirrT2U.csv");

T = max(data.loandayselapsed)+1;
N = height(data)/T;

cf = reshape(data.cf,T,N);

% Calculate IRRs for individual loans

irrbyloan = irr(cf);
irrbyloan(isnan(irrbyloan))=-inf;
irrbyloan = irrbyloan';

% Re-arrange loans

irrbyloan = [irrbyloan [1:length(irrbyloan)]'];
irrbyloan = sortrows(irrbyloan,1);
cf = cf(:,irrbyloan(:,2)');

% Group into quantiles

cf_quantiles = NaN(size(cf,1),N_Quantiles);
for d=1:N_Quantiles
    lowind = ceil(size(cf,2)/N_Quantiles)*(d-1)+1;
    highind = min((ceil(size(cf,2)/N_Quantiles)*d),size(cf,2));
    cf_quantiles(:,d) = sum(cf(:,lowind:highind),2);
end

% IRR by quantiles

irrbyquantiles = irr(cf_quantiles);

% IRR overall

irrall = irr(sum(cf,2));

% Export data for plotting

irrbyloan_T2U = irrbyloan;
irrbyquantiles_T2U = irrbyquantiles;

%--------------------------%
% Bootstrap & calculate SE %
%--------------------------%

irrbyquantiles_allboots = NaN(N_boots,4);

for i=1:N_boots

    cf_new = cf(:,randsample(1:size(cf,2),size(cf,2),true));

    % Calculate IRRs for individual loans

    irrbyloan = irr(cf_new);
    irrbyloan(isnan(irrbyloan))=-inf;
    irrbyloan = irrbyloan';

    % Re-arrange loans

    irrbyloan = [irrbyloan [1:length(irrbyloan)]'];
    irrbyloan = sortrows(irrbyloan,1);
    cf_new = cf_new(:,irrbyloan(:,2)');

    % Calculate IRR on a portfolio of all loans

    cf_allloanportfolio = sum(cf_new,2);
    irr_allloanportfolio = irr(cf_allloanportfolio);
    irrbyquantiles_allboots(i,4) = irr_allloanportfolio;

    % Group into quantiles

    cf_quantiles = NaN(size(cf_new,1),N_Quantiles);
    for d=1:N_Quantiles
        lowind = ceil(size(cf_new,2)/N_Quantiles)*(d-1)+1;
        highind = min((ceil(size(cf_new,2)/N_Quantiles)*d),size(cf_new,2));
        cf_quantiles(:,d) = sum(cf_new(:,lowind:highind),2);
    end

    % IRR by quantiles

    irrbyquantiles_oneboot = irr(cf_quantiles);

    irrbyquantiles_allboots(i,1:3) = irrbyquantiles_oneboot;

end

irrbyquantiles_allboots_T2U = irrbyquantiles_allboots;

%--------------------%
% Export the outputs %
%--------------------%

save("../data/interim/irrbyquantiles_allboots_T1L.mat",'irrbyquantiles_allboots_T1L');
save("../data/interim/irrbyquantiles_allboots_T2U.mat",'irrbyquantiles_allboots_T2U');
save("../data/interim/irrbyloan_T1L.mat",'irrbyloan_T1L');
save("../data/interim/irrbyloan_T2U.mat",'irrbyloan_T2U');




%%%%%%%%%%%%%%%%%%
% Only Old Group %
%%%%%%%%%%%%%%%%%%

data = readtable("../data/interim/loanlevelirrOld.csv");

T = max(data.loandayselapsed)+1;
N = height(data)/T;

cf = reshape(data.cf,T,N);

% Calculate IRRs for individual loans

irrbyloan = irr(cf);
irrbyloan(isnan(irrbyloan))=-inf;
irrbyloan = irrbyloan';

% Re-arrange loans

irrbyloan = [irrbyloan [1:length(irrbyloan)]'];
irrbyloan = sortrows(irrbyloan,1);
cf = cf(:,irrbyloan(:,2)');

% Group into quantiles

cf_quantiles = NaN(size(cf,1),N_Quantiles);
for d=1:N_Quantiles
    lowind = ceil(size(cf,2)/N_Quantiles)*(d-1)+1;
    highind = min((ceil(size(cf,2)/N_Quantiles)*d),size(cf,2));
    cf_quantiles(:,d) = sum(cf(:,lowind:highind),2);
end

% IRR by quantiles

irrbyquantiles = irr(cf_quantiles);

% IRR overall

irrall = irr(sum(cf,2));

% Export data for plotting

irrbyloan_Old = irrbyloan;
irrbyquantiles_Old = irrbyquantiles;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Load back data from bootstrapping %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

load("../data/interim/irrbyquantiles_allboots_T1L.mat");
load("../data/interim/irrbyquantiles_allboots_T2U.mat");
load("../data/interim/irrbyloan_T1L.mat");
load("../data/interim/irrbyloan_T2U.mat");

% Import these as "rl6_loanlevelirr_old.m" has generated these already

load("../data/interim/irrbyquantiles_allboots_Old.mat");
load("../data/interim/irrbyloan_Old.mat");



%%%%%%%%%%%%
% Plotting %
%%%%%%%%%%%%

%--------------------%
% IRRs of portfolios %
%--------------------%


figure('DefaultAxesFontSize',12);fig=gcf;fig.Position(3:4)=[600,350];
plot(1:N_Quantiles,(1+irrbyquantiles_T1L).^30*100-100,'LineWidth',1,'Marker','^','MarkerSize',4,'Color','red','MarkerFaceColor','red');
hold on;
plot(1:N_Quantiles,(1+irrbyquantiles_T2U).^30*100-100,'LineWidth',1,'Marker','o','MarkerSize',4,'Color','green','MarkerFaceColor','green');
hold on;
plot(1:N_Quantiles,(1+irrbyquantiles_Old).^30*100-100,'LineWidth',1,'Marker','diamond','MarkerSize',4,'Color','black','MarkerFaceColor','black');
hold on;
xticks(1:N_Quantiles); xticklabels(1:N_Quantiles);
ylim([-60,25]); xlim([0.9,3.5]);

errorbar(1:N_Quantiles,(1+irrbyquantiles_T1L).^30*100-100,[std(irrbyquantiles_allboots_T1L(:,1))*1.96,std(irrbyquantiles_allboots_T1L(:,2))*1.96,std(irrbyquantiles_allboots_T1L(:,3))*1.96]*30*100, ...
    "LineStyle","none",'LineWidth',1,'Color','red');
errorbar(1:N_Quantiles,(1+irrbyquantiles_T2U).^30*100-100,[std(irrbyquantiles_allboots_T2U(:,1))*1.96,std(irrbyquantiles_allboots_T2U(:,2))*1.96,std(irrbyquantiles_allboots_T2U(:,3))*1.96]*30*100, ...
    "LineStyle","none",'LineWidth',1,'Color','green');
errorbar(1:N_Quantiles,(1+irrbyquantiles_Old).^30*100-100,[std(irrbyquantiles_allboots_Old(:,1))*1.96,std(irrbyquantiles_allboots_Old(:,2))*1.96,std(irrbyquantiles_allboots_Old(:,3))*1.96]*30*100, ...
    "LineStyle","none",'LineWidth',1,'Color','black');

text(1+0.02,(1+irrbyquantiles_T1L(1))^30*100-100,strcat(compose('%0.0f',(1+irrbyquantiles_T1L(1))^30*100-100),'%'),'VerticalAlignment','top','FontSize',12);
text(2-0.04,(1+irrbyquantiles_T1L(2))^30*100-100,strcat(compose('%0.0f',(1+irrbyquantiles_T1L(2))^30*100-100),'%'),'VerticalAlignment','middle','HorizontalAlignment','right','FontSize',12);
text(3+0.05,(1+irrbyquantiles_T1L(3))^30*100-100,strcat(compose('%0.0f',(1+irrbyquantiles_T1L(3))^30*100-100),'%'),'VerticalAlignment','middle','HorizontalAlignment','left','FontSize',12);

text(1+0.02,(1+irrbyquantiles_T2U(1))^30*100-100,strcat(compose('%0.0f',(1+irrbyquantiles_T2U(1))^30*100-100),'%'),'VerticalAlignment','top','FontSize',12);
text(2-0.04,(1+irrbyquantiles_T2U(2))^30*100-100,strcat(compose('%0.0f',(1+irrbyquantiles_T2U(2))^30*100-100),'%'),'VerticalAlignment','top','FontSize',12);
text(3,(1+irrbyquantiles_T2U(3))^30*100-100-2,strcat(compose('%0.0f',(1+irrbyquantiles_T2U(3))^30*100-100),'%'),'VerticalAlignment','top','HorizontalAlignment','center','FontSize',12);

text(1+0.02,(1+irrbyquantiles_Old(1))^30*100-100,strcat(compose('%0.0f',(1+irrbyquantiles_Old(1))^30*100-100),'%'),'VerticalAlignment','top','HorizontalAlignment','left','FontSize',12);
text(2,(1+irrbyquantiles_Old(2))^30*100-100,strcat(compose('%0.0f',(1+irrbyquantiles_Old(2))^30*100-100),'%'),'VerticalAlignment','bottom','HorizontalAlignment','right','FontSize',12);
text(3,(1+irrbyquantiles_Old(3))^30*100-100+2,strcat(compose('%0.0f',(1+irrbyquantiles_Old(3))^30*100-100),'%'),'VerticalAlignment','bottom','HorizontalAlignment','center','FontSize',12);

ylabel({'Monthly IRR of portfolio of loans (%)'});xlabel('Terciles by the IRR of individual loans');
legend({'Secured','Unsecured',"Prior School-Fee"+newline+"Loans (Secured)"},'Location','southeast');legend('boxoff')
saveas(gcf,'../figures/IRRs_terciles.png');
saveas(gcf,'../figures/IRRs_terciles.eps','epsc');
hold off;
clf('reset'); close all;



%-------------------%
% Histogram of IRRs %
%-------------------%

edges = -34.5:1:24.5; 
N_bins = length(edges)+1; % beyond two edges
irr_hist_T1L = NaN(N_bins,1);
irr_hist_T2U = NaN(N_bins,1);
irr_hist_Old = NaN(N_bins,1);
% Position of dots
dotPos = [-35:1:25];

monthlyirr = (1+irrbyloan_T1L(:,1)).^30*100-100;
monthlyirr(monthlyirr==Inf) = -Inf;
irr_hist_T1L(1) = sum(monthlyirr<=edges(1)|monthlyirr==-inf);
% For the bins in between edges
for i=1:length(edges)-1
    irr_hist_T1L(i+1) = sum(monthlyirr>edges(i)&monthlyirr<=edges(i+1));
end
irr_hist_T1L(end) = sum(monthlyirr>edges(end)&monthlyirr~=-inf);
irr_hist_T1L = irr_hist_T1L/length(irrbyloan_T1L);

monthlyirr = (1+irrbyloan_T2U(:,1)).^30*100-100;
monthlyirr(monthlyirr==Inf) = -Inf;
irr_hist_T2U(1) = sum(monthlyirr<=edges(1)|monthlyirr==-inf);
% For the bins in between edges
for i=1:length(edges)-1
    irr_hist_T2U(i+1) = sum(monthlyirr>edges(i)&monthlyirr<=edges(i+1));
end
irr_hist_T2U(end) = sum(monthlyirr>edges(end)&monthlyirr~=-inf);
irr_hist_T2U = irr_hist_T2U/length(irrbyloan_T2U);

monthlyirr = (1+irrbyloan_Old(:,1)).^30*100-100;
monthlyirr(monthlyirr==Inf) = -Inf;
irr_hist_Old(1) = sum(monthlyirr<=edges(1)|monthlyirr==-inf);
% For the bins in between edges
for i=1:length(edges)-1
    irr_hist_Old(i+1) = sum(monthlyirr>edges(i)&monthlyirr<=edges(i+1));
end
irr_hist_Old(end) = sum(monthlyirr>edges(end)&monthlyirr~=-inf);
irr_hist_Old = irr_hist_Old/length(irrbyloan_Old);

% Position of labels
labelPos = -35:5:25;
labels = cellstr(string([labelPos]));
labels{1}='<-30%';
labels{end}='>20%';



%-----%
% CDF %
%-----%

figure('DefaultAxesFontSize',12);fig=gcf;fig.Position(3:4)=[600,350];
plot(dotPos,cumsum(irr_hist_T1L),'Marker','none','MarkerSize',3,'LineWidth',1,'Color','red');
hold on;
plot(dotPos,cumsum(irr_hist_T2U),'Marker','none','MarkerSize',3,'LineWidth',1,'Color','green');
hold on;
plot(dotPos,cumsum(irr_hist_Old),'Marker','none','MarkerSize',3,'LineWidth',1,'Color','black');
hold on;
xticks(labelPos); xticklabels(labels);
ylim([0,1]);
ylabel({'Cumulative probability density'});xlabel('Monthly IRR of loans (%)');
legend({'Secured','Unsecured',"Prior School-Fee"+newline+"Loans (Secured)"},'Location','southeast');legend('boxoff')
saveas(gcf,'../figures/IRRs_cdf.png');
saveas(gcf,'../figures/IRRs_cdf.eps','epsc');
hold off;
clf('reset'); close all;



